package com.company;

public class TestSync implements Runnable {
    private int balance;


    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        for (int i = 0; i < 50; i++) {
            increment();
            // 打印函数是同步的
            synchronized (this) {
                System.out.println(name + " balance is " + balance);
            }
        }
    }

    synchronized void increment() {
        int i = balance;
        balance = i + 1;
    }
}
